package com.example.wallpaperapp.ui.activity;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wallpaperapp.R;
import com.example.wallpaperapp.base.BaseActivity;
import com.example.wallpaperapp.bean.UserInfo;
import com.example.wallpaperapp.ihttps.RetrofitService;
import com.example.wallpaperapp.p.LoginP;
import com.example.wallpaperapp.p.LoginPresent;
import com.example.wallpaperapp.utils.IntentUtil;
import com.example.wallpaperapp.utils.SpfUtils;
import com.example.wallpaperapp.utils.StatusBar;
import com.example.wallpaperapp.utils.ToastUtil;
import com.example.wallpaperapp.v.LoginV;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginActivity extends BaseActivity implements LoginV, View.OnClickListener {

    @BindView(R.id.toSign)
    TextView toSign;
    @BindView(R.id.et_num)
    EditText et_num;
    @BindView(R.id.et_pwd)
    EditText et_pwd;
    @BindView(R.id.bt_sign)
    Button bt_sign;
    @BindView(R.id.login_bar)
    TextView login_bar;
    @BindView(R.id.login_back)
    TextView login_back;
    String a;
    LoginP present;

    @Override
    protected int initLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) login_bar.getLayoutParams();
        layoutParams.height = StatusBar.getStatusBarHeight(this);
        login_bar.setLayoutParams(layoutParams);
    }
    @Override
    protected void initData() {
        //定义本地存储
        SpfUtils.init(LoginActivity.this);

        present = new LoginPresent(this);
        login_back.setOnClickListener(this);
        bt_sign.setOnClickListener(this);
        toSign.setOnClickListener(this);
    }
    @Override
    public int getUserName() {
        if (a.isEmpty()) {
            return 0;
        } else {
            return Integer.parseInt(a);
        }
    }

    @Override
    public String getPassword() {
        return et_pwd.getText().toString().trim();
    }

    @Override
    public void onFailed(String msg) {
        if (msg != null) {
            ToastUtil.makeText(LoginActivity.this, msg);

        }
    }

    @Override
    public void onSuccess(String msg) {
        if (msg != null) {
            ToastUtil.makeText(LoginActivity.this, msg);
            //finish();
        }
    }

    @Override
    public void result(int id) {
        Log.d(TAG, "result: id" + id);
        SpfUtils.setParam("userId",id);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_back:
                finish();
                break;
            case R.id.bt_sign:
                a = et_num.getEditableText().toString().trim();
                present.requestMsg();
                break;
            case R.id.toSign:
                //跳转到注册页面
                IntentUtil.startActivity(SignInActivity.class, LoginActivity.this);
                break;

        }
    }
}